package com.lmh.ymw.controller.user;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lmh.ymw.entity.RespBean;
import com.lmh.ymw.entity.User;
import com.lmh.ymw.service.user.UserService;
import com.lmh.ymw.util.SendEmail;
import com.lmh.ymw.util.httpapidemo.SmsApiHttpSendTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    @Autowired
    UserService service;
    private String yz="";
    @PostMapping("/login")
    public RespBean login(@RequestParam("name") String name, @RequestParam String pwd){
        User user = service.getUser(name, pwd);
        System.out.println(name);
        if (user != null) {
            System.out.println(1);
            return RespBean.ok("登陆成功",user);
        }else {
            return RespBean.error("登录失败");
        }
    }
    @PostMapping("/sjLogin")
    public RespBean sjLogin(@RequestParam("name") String sj){
        User user = service.getSj(sj);
        return RespBean.ok("登陆成功",user);
    }
    @PostMapping("/hqyz")
    public RespBean hqyz(@RequestParam("name") String sj){
        User user = service.getSj(sj);
        /*邮箱验证*/
  /*      yz= SendEmail.yzm("582072714@qq.com");*/
        /*手机验证*/
        yz= SmsApiHttpSendTest.yzm(sj);

        if (user != null) {
            return RespBean.ok(yz,user);
        }else {
            return RespBean.error("手机号错误");
        }
    }

    @PostMapping("/")
    public RespBean add(User u){
        boolean add = service.add(u);
        if (add ==true) {
            return RespBean.ok("添加成功");
        }else {
            return RespBean.error("添加失败");
        }
    }


    @PutMapping("/")
    public RespBean up(User user){
        boolean update = service.update(user);
        if (update ==true) {
            return RespBean.ok("修改成功");
        }else {
            return RespBean.error("修改失败");
        }
    }
    @GetMapping("/list")
    public RespBean list(@RequestParam Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<User> userList = service.getUserList();
        PageInfo<User> list=new PageInfo<User>(userList);
        return RespBean.ok("查询成功",list);
    }

    @DeleteMapping("/{id}")
    public RespBean del(@PathVariable Integer id){
        boolean b = service.deleteUserById(id);
        if (b ==true) {
            return RespBean.ok("删除成功");
        }else {
            return RespBean.error("删除失败");
        }
    }


}
